<template>
    <div>
      <Row class="search-info-style">
        <Col span="6">
          <span>类&ensp;&ensp;&ensp;别&ensp;：</span>
          <Select v-model="searchInfo.category" clearable style="width:70%">
            <Option value="">全部</Option>
            <Option value="1" >工作服务类</Option>
            <Option value="2" >评比表彰类</Option>
            <Option value="3" >创建示范类</Option>
            <Option value="4" >标识类</Option>
            <Option value="0" >其他</Option>
          </Select>
        </Col>
        <Col span="6">
            <span>级&ensp;&ensp;&ensp;别&ensp;：</span>
            <Select v-model="searchInfo.level" clearable style="width:70%">
              <Option value="">全部</Option>
              <Option value="1">市级</Option>
              <Option value="2">区县</Option>
              <Option value="3">街道</Option>
            </Select>
          </Col>
          <Col span="6">
            <span>位&ensp;&ensp;&ensp;置&ensp;：</span>
            <Select v-model="searchInfo.room" clearable style="width:70%">
              <Option value="">全部</Option>
              <Option value="0">室内</Option>
              <Option value="1">室外</Option>
            </Select>
          </Col>
          <Col style="float:right" pull="1">
              <!-- <Button class="new-btn-sty" @click="add({})"><Icon type="md-add" /> 添加</Button> -->
              <Button class="new-btn-sty" @click="findtime()"><Icon type="ios-search" /> 搜索</Button>
              <Button class="new-btn-sty" @click="see()"><Icon type="md-browsers" /> 查看社区室外牌子</Button>
          </Col>
          <!-- <Col span="2" style="float:right" pull="1">
              <Button class="new-btn-sty" @click="findtime()"><Icon type="ios-search" /> 搜索</Button>
          </Col> -->
      </Row>
      <Row class="search-info-style">
        <Col span="6">
            <span>牌子名称：</span>
            <Input v-model="searchInfo.brand_name" placeholder="牌子名称" style="width:70%" />
        </Col>
        <!-- <Col span="2" style="float:right" pull="1">
              <Button class="new-btn-sty" @click="see()"><Icon type="ios-search" /> 查看社区室外牌子</Button>
        </Col> -->
      </Row>
      <Table border :columns="columns1" :data="datalist" ></Table>
      <Row v-if="loadData == true">
        <Col class="demo-spin-col" span="8" offset="8">
          <Spin fix>
            <Icon type="ios-loading" size=18 class="demo-spin-icon-load"></Icon>
            <div>数据正在加载</div>
          </Spin>
        </Col>
      </Row>
      <Page class="page_style" :total="searchInfo.totalnum" :current="searchInfo.currentPageNo"
              :page-size="searchInfo.currentPageSize" @on-change="changePageNo" show-total  show-sizer @on-page-size-change="size"/>
      <Modal v-model="modalEdit" title="详细信息" width="700" cancel-text="关闭"
        @on-ok="ok111(paramData2)" @on-cancel="modalEdit=false" ok-text="确定">
        <Form :model="paramData2" >
          <Row class="item1">
            <Col span="5" style="text-align: right" class="">挂牌机构:</Col>
            <Col span="12" offset="1">
              <Input v-model="paramData2.community_name" disabled></Input>
            </Col>
          </Row>
          <Row class="item1" style="height:auto;line-height:auto">
            <Col span="5" style="text-align: right" class="">牌子名称:</Col>
            <Col span="12" offset="1">
              <Input v-model="paramData2.brand_name" disabled></Input>
            </Col>
          </Row>
          <Row class="item1" style="height:auto;line-height:auto">
            <Col span="5" style="text-align: right" class="">创建机构:</Col>
            <Col span="12" offset="1">
              <Input v-model="paramData2.create_name" disabled></Input>
            </Col>
          </Row>
          <Row class="item1">
            <Col span="5" style="text-align: right" class="">类别:</Col>
            <Col span="12" offset="1">
            <!-- 1.工作服务类/2.评比表彰类/3.创建示范类/0.其他 -->
                <Select v-model="paramData2.category" disabled>
                  <Option value="0">其他</Option>
                  <Option value="1">工作服务类</Option>
                  <Option value="2">评比表彰类</Option>
                  <Option value="3">创建示范类</Option>
                  <Option value="4" >标识类</Option>
                </Select>
              </Col>
          </Row>
          <Row class="item1">
            <Col span="5" style="text-align: right" class="">位置:</Col>
            <Col span="12" offset="1">
                <Select v-model="paramData2.room" disabled>
                  <Option value="0">室内</Option>
                  <Option value="1">室外</Option>
                </Select>
              </Col>
          </Row>
          <Row class="item1">
            <Col span="5" style="text-align: right" class="">牌子级别:</Col>
            <Col span="12" offset="1">
                <Select v-model="paramData2.level" disabled>
                  <Option value="1">市级</Option>
                  <Option value="2">区/县</Option>
                  <Option value="3">街道</Option>
                </Select>
              </Col>
          </Row>
          <Row  class="item1">
            <Col span="5" style="text-align: right" class="">颁发时间:</Col>
            <Col span="12" offset="1">
              <DatePicker type="datetime" format="yyyy-MM-dd HH:mm" v-model="paramData2.create_time" placeholder="颁发时间" style="width: 100%" disabled></DatePicker>
            </Col>
          </Row>
          <Row class="" style="">
            <Col span="5" style="text-align: right" class="">牌子照片:</Col>
            <Col span="12" offset="1">
                <img :src="showphoto" class="hd-img-responsive person-info-header"/>
                <!-- <Icon @click="updatePhoto" type="md-camera" size="40" style="position:absolute;z-index:100;left:330px;top: 150px"/> -->
              </Col>
          </Row>
        </Form>
      </Modal>
      <Modal v-model="up_pic_mol" title="图片上传" :closable="false" @on-ok="ok222(paramData)" @on-cancel="up_pic_mol=false" ok-text="确定">
      <Upload multiple action="http://10.40.20.4:8081/api/uc/mzsc/uploadService/uploadPic" :before-upload="beforeUpload">
        <Button icon="ios-cloud-upload-outline">上传图片</Button>
        <span style="margin-left: 250px">请上传小于2M的图片</span>
      </Upload>
      <img :src="temp_photo" style="width: 100%"/>
      </Modal>
      <Modal v-model="modalEdit2" title="牌子信息" width="700" scrollable cancel-text="关闭"
        @on-ok="ok333(paramData2)" @on-cancel="modalEdit2=false" ok-text="确定">
        <Form :model="paramData2" >
          <Row class="item1" style="height:auto;line-height:auto">
            <Col span="5" style="text-align: right" class="">牌子名称:</Col>
            <Col span="15" offset="1">
              <Input v-model="paramData2.brand_name"></Input>
            </Col>
          </Row>
          <Row class="item1">
            <Col span="5" style="text-align: right" class="">牌子类别:</Col>
            <Col span="15" offset="1">
            <!-- 1.工作服务类/2.评比表彰类/3.创建示范类/0.其他 -->
                <Select v-model="paramData2.category">
                  <Option value="1">工作服务类</Option>
                  <Option value="2">评比表彰类</Option>
                  <Option value="3">创建示范类</Option>
                  <Option value="4" >标识类</Option>
                  <Option value="0">其他</Option>
                </Select>
              </Col>
          </Row>
          <Row class="item1">
            <Col span="5" style="text-align: right" class="">位置:</Col>
            <Col span="15" offset="1">
                <Select v-model="paramData2.room">
                  <Option value="0">室内</Option>
                  <Option value="1">室外</Option>
                </Select>
              </Col>
          </Row>
          <Row class="item1">
            <Col span="5" style="text-align: right" class="">牌子级别:</Col>
            <Col span="15" offset="1">
                <Select v-model="paramData2.level">
                  <Option value="1">市级</Option>
                  <Option value="2">区/县</Option>
                  <Option value="3">街道</Option>
                </Select>
              </Col>
          </Row>
          <Row>
          <Col span="5" style="text-align: right" class="">发牌单位：</Col>
          <Col span="15" offset="1">
            <CheckboxGroup v-model="paramData2.licensing">
              <Checkbox label="1" border style="margin-top: 4px">市委组织部</Checkbox>
              <Checkbox label="2" border style="margin-top: 4px">市民政局</Checkbox>
              <Checkbox label="3" border style="margin-top: 4px">市农业农村局</Checkbox>
              <Checkbox label="4" border style="margin-top: 4px">市公安局</Checkbox>
              <Checkbox label="5" border style="margin-top: 4px">市委宣传部</Checkbox>
              <Checkbox label="6" border style="margin-top: 4px">市委政法委</Checkbox>
              <Checkbox label="7" border style="margin-top: 4px">市司法局</Checkbox>
              <Checkbox label="8" border style="margin-top: 4px">市文化旅游广电局</Checkbox>
              <Checkbox label="9" border style="margin-top: 4px">市退役军人局</Checkbox>
              <Checkbox label="10" border style="margin-top: 4px">沈阳警备区</Checkbox>
            </CheckboxGroup>
          </Col>
        </Row>
          <Row class="" style="">
            <Col span="5" style="text-align: right" class="">牌子照片:</Col>
            <!-- <div> -->
            <Col span="12" offset="1">
              <img :src="showphoto" class="hd-img-responsive person-info-header"/>
              <Button @click="updatePhoto" size="50" style="position:absolute;z-index:100;left:340px;top: 10px"><Icon type="md-camera" />
                <!-- <Icon @click="updatePhoto" type="md-camera" size="40" style="position:absolute;z-index:100;left:340px;top: 10px"/> -->
                点击上传照片</Button>
              </Col>
            <!-- </div> -->
          </Row>
        </Form>
      </Modal>
      <!-- 查看社区牌子 -->
      <Modal v-model="modalEdit3" title="社区室外牌子信息" width="700" scrollable cancel-text="关闭"
        @on-ok="OKOK(paramData3)" @on-cancel="modalEdit3=false" ok-text="确定">
        <Form :model="paramData3" >
          <Row class="" style="">
            <Col span="5" style="text-align: right" class="">牌子照片:</Col>
            <!-- <div> -->
            <Col span="12" offset="1">
              <img :src="showphoto" class="hd-img-responsive person-info-header"/>
              <Button @click="updatePhoto" size="50" style="position:absolute;z-index:100;left:340px;top: 10px"><Icon type="md-camera" />
                <!-- <Icon @click="updatePhoto" type="md-camera" size="40" style="position:absolute;z-index:100;left:340px;top: 10px"/> -->
                点击上传照片</Button>
              </Col>
            <!-- </div> -->
          </Row>
        </Form>
      </Modal>
    </div>
</template>
<script>
import nophoto from '@/assets/images/nophoto.jpeg'
import { findMyBrandList, date0str, updatePhoto, saveUnqualifiedBrand, addCommunityBrand, getCommunityPhoto } from '@/api/eventlist'
import { createUpLoadFiles, getSign } from '@/api/common'
export default {
  data () {
    return {
      temp_photo: '',
      paramData: {},
      modalEdit2: false,
      modalEdit3: false,
      userInfo: { photo: nophoto },
      showphoto: '',
      PhotoModal: false,
      up_pic_mol: false,
      upPicData: {
        fileName: '',
        filePath: ''
      },
      searchInfo: {
        applyname: '',
        order_code: '',
        applytime: [],
        totalnum: 0,
        currentPageNo: 1,
        currentPageSize: 10,
        department_name: ''
      },
      paramData3: { id: '' },
      paramData2: { id: '' },
      modalEdit: false,
      loadData: true,
      param: {},
      datalist: [ ],
      columns1: [
        { title: '序号', type: 'index', key: 'index', width: 100, align: 'center' },
        { title: '机构名称', key: 'company_code_name', width: 200, align: 'center' },
        { title: '牌子名称', key: 'brand_name', width: 200, align: 'center' },
        { title: '颁发时间', key: 'create_time', width: 200, align: 'center' },
        // { title: '失效时间', key: 'invalid_time', width: 200, align: 'center', sortable: true },
        { title: '位置', key: 'room_name', width: 150, align: 'center' },
        { title: '级别', key: 'level_name', width: 150, align: 'center' },
        { title: '是否上传图片', key: 'photo', width: 100, align: 'center' },
        { title: '牌子来源', key: 'grade_name', width: 100, align: 'center' },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          className: 'nopadding',
          minWidth: 150,
          render: (h, params) => {
            let temp = []
            temp.push(
              h('Button', {
                props: { type: 'primary', size: 'small' },
                style: { marginRight: '5px', backgroundColor: '#598ED1', borderColor: '#598ED1' },
                on: {
                  click: () => {
                    this.editNotice(params.row)
                  }
                }
              }, '详情'),
              h('Button', {
                props: { type: 'error', size: 'small' },
                style: { marginRight: '5px' },
                on: {
                  click: () => {
                    this.updatePhoto(params.row)
                  }
                }
              }, '上传照片')
            )
            if (params.row.grade === '1') {
              temp.push(
                h('Button', {
                  props: { type: 'primary', size: 'small' },
                  style: { marginRight: '5px', backgroundColor: '#598ED1', borderColor: '#598ED1' },
                  on: {
                    click: () => {
                      this.add(params.row)
                    }
                  }
                }, '编辑')
              )
            }
            //  else if (params.row.grade === '0') {
            //   temp.push(
            //     h('Button', {
            //       props: { type: 'error', size: 'small' },
            //       style: { marginRight: '5px' },
            //       on: {
            //         click: () => {
            //           this.updatePhoto(params.row)
            //         }
            //       }
            //     }, '上传照片')
            //   )
            // }
            return h('div', temp)
          }
        }
      ]
    }
  },
  methods: {
    init (param) {
      param.pageNo = this.searchInfo.currentPageNo
      param.pageSize = this.searchInfo.currentPageSize
      findMyBrandList(param).then(res => {
        if (res.data.errcode === 0) {
          this.datalist = res.data.data.results
          this.searchInfo.totalnum = res.data.data.totalRecord
          this.datalist.forEach(function (item1, index) {
            if (item1.room === '0') {
              item1.room_name = '室内'
            } else if (item1.room === '1') {
              item1.room_name = '室外'
            }
            if (item1.subsidiary_name) {
              item1.brand_name = item1.subsidiary_name
            }
            if (item1.brand_photo) {
              item1.photo = '是'
            } else {
              item1.photo = '否'
            }
          })
          this.loadData = false
        } else {
          this.loadData = true
          this.$Notice.error({ title: '获取发起列表失败', desc: res.data.errmsg })
        }
      })
    },
    size (pageSize) {
      this.searchInfo.currentPageSize = pageSize
      this.changePageNo()
    },
    changePageNo (pageNo) {
      this.searchInfo.currentPageNo = pageNo
      this.init(this.param)
    },
    findtime () {
      this.param.level = this.searchInfo.level
      this.param.category = this.searchInfo.category
      this.param.room = this.searchInfo.room
      this.init(this.param)
    },
    updatePhoto (item) {
      this.paramData = item
      this.up_pic_mol = true
      this.PhotoModal = false
    },
    beforeUpload (file) {
      this.upPicData.fileName = this.upPicData.fileName + '.' + file.type.substring(6)
      let that = this
      const reader = new FileReader()
      reader.readAsDataURL(file)
      reader.onload = () => {
        let index = reader.result.indexOf('base64,') + 7
        that.upPicData.file = reader.result.substring(index)
        that.upPic(that.upPicData)
      }
      return false
    },
    upPic (obj) {
      var time = new Date().getTime()
      this.timeStep = time.toString()
      let param = {
        filePath: obj.filePath,
        fileName: obj.fileName,
        timestamp: this.timeStep
      }
      getSign(param).then(res => {
        if (res.data.errcode === 0) {
          createUpLoadFiles(res.data.data.docking_url, { sign: res.data.data.sign, timestamp: this.timeStep, filePath: obj.filePath, fileName: obj.fileName, file: obj.file }).then(res => {
            if (res.data.errcode === 0) {
              let imgurl = res.data.data
              this.temp_photo = imgurl
              this.showphoto = imgurl
              this.userInfo.photo = imgurl
            } else {
              this.$Notice.error({ title: '上传失败', desc: res.data.data.msg })
            }
          })
        } else {
          this.$Notice.error({ title: '上传失败', desc: res.data.data.msg })
        }
      })
    },
    editNotice (item) {
      this.modalEdit = true
      let create_time = new Date()
      if (item.create_time) {
        create_time = date0str(item.create_time)
      }
      if (item.brand_photo) {
        this.showphoto = item.brand_photo
      } else {
        this.showphoto = this.userInfo.photo
      }
      this.paramData2.id = item.id
      this.paramData2.create_time = create_time
      this.paramData2.community_name = item.company_code_name
      this.paramData2.brand_name = item.brand_name
      this.paramData2.create_name = item.create_by_name
      this.paramData2.room = item.room
      this.paramData2.category = item.category
      this.paramData2.level = item.level
    },
    ok111 () {
      this.modalEdit = false
    },
    ok222 (param) {
      if (this.showphoto) {
        param.brand_photo = this.showphoto
        updatePhoto(param).then(res => {
          if (res.data.errcode === 0) {
            this.$Notice.success({ title: '图片上传成功', desc: '' })
            this.temp_photo = ''
            this.init(this.param)
            this.userInfo.photo = nophoto
          }
        })
      } else {
        this.$Notice.error({ title: '上传失败', desc: '不能上传空图片' })
      }
    },
    add (item) {
      this.modalEdit2 = true
      if (item.brand_photo) {
        this.showphoto = item.brand_photo
      } else {
        this.showphoto = this.userInfo.photo
      }
      this.paramData2.brand_id = item.brand_id || null
      this.paramData2.id = item.id || null
      this.paramData2.brand_name = item.brand_name || ''
      this.paramData2.category = item.category || ''
      this.paramData2.room = item.room || ''
      this.paramData2.level = item.level || ''
      // this.showphoto = item.brand_photo || ''
    },
    ok333 (param) {
      param.brand_photo = this.showphoto
      let str = ''
      for (let i = 0; i < param.licensing.length; i++) {
        str += param.licensing[i] + ','
      }
      if (str.length > 0) {
        str = str.substr(0, str.length - 1)
      }
      param.licensing = str
      saveUnqualifiedBrand(param).then(res => {
        if (res.data.errcode === 0) {
          this.init(this.param)
          this.temp_photo = ''
          this.paramData2.licensing = ''
          this.$Notice.success({ title: '成功', desc: '' })
        } else {
          this.$Notice.error({ title: '失败', desc: res.data.errmsg })
        }
      })
      this.init(this.param)
    },
    see () {
      this.modalEdit3 = true
      getCommunityPhoto().then(res => {
        this.showphoto = this.userInfo.photo
        if (res.data.errcode === 0) {
          this.showphoto = res.data.data.brand_photo
          this.paramData3.id = res.data.data.id
        }
      })
    },
    OKOK (param) {
      if (this.showphoto) {
        param.id = this.paramData3.id
        param.brand_photo = this.showphoto
        addCommunityBrand(param).then(res => {
          if (res.data.errcode === 0) {
            this.$Notice.success({ title: '成功', desc: '' })
          } else {
            this.$Notice.error({ title: '失败', desc: res.data.errmsg })
          }
        })
      } else {
        this.$Notice.error({ title: '上传失败', desc: '不能上传空图片' })
      }
    }
  },
  mounted () {
    this.init(this.param)
    this.showphoto = this.userInfo.photo
  }
}
</script>
<style>
  .page_style {
    margin-top: 10px;
    text-align: right;
  }
  .search-info-style {
    margin: 10px;
  }
  .item1{
    height: 42px;
    line-height: 40px;
  }
  .new-btn-sty {
    margin-right: 5px;
    color: #fff;
    background-color: #235CA7;
    border-color: #235CA7;
    font-weight: 500;
  }
</style>
